package com.fg.cloud.system.controller.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.construction.Tree;
import com.fg.cloud.common.finals.MenuClassify;
import com.fg.cloud.common.finals.UserType;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.system.MenuEntity;
import com.fg.cloud.manager.entry.system.vo.SystemUserVo;
import com.fg.cloud.manager.service.system.MenuService;
import com.fg.cloud.system.controller.BaseSystemController;

/**
 * Description:系统平台菜单管理
 * 
 * @author shaoqiping
 * @date 2017年10月24日下午5:49:37
 */
@Controller
@RequestMapping("systemMenu")
public class SystemMenuController extends BaseSystemController {

	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "/menu")
	public String findAll() {
		return "system/menu";
	}

	@RequestMapping(value = "")
	public String menu() {
		if (!SecurityUtils.getSubject().isAuthenticated()) {
			return "login";
		} else if (!UserType.SYSTEM.equals(getCurrentSystemUser().getType())) {
			return "login";
		}
		return "system/menu";
	}

	/**
	 * Description:页面左侧菜单
	 * 
	 * @param request
	 * @param response
	 * @author shaoqiping
	 * @date 2017年10月24日下午5:58:13
	 */
	@RequiresRoles(UserType.SYSTEM)
	@RequestMapping(value = "/system", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void findLeftMenu(HttpServletRequest request, HttpServletResponse response) {
		try {
			SystemUserVo user = getCurrentSystemUser();
			if (user == null) {
				doResult(response, false, StatusCode.LOGINOUT, null, ResultType.LOGINOUT);
				return;
			}
			doResult(response, true, StatusCode.FINDSUCCESS, menuService.findList(MenuClassify.MENU_SYSTEM),
					ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * Description:显示所有菜单
	 * 
	 * @param request
	 * @param response
	 * @author shaoqiping
	 * @date 2017年10月24日下午5:58:13
	 */
	@RequiresRoles(UserType.SYSTEM)
	@RequestMapping(value = "/menuList", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<Tree> findLeftMenu(HttpServletRequest request, HttpServletResponse response, int classify) {
		try {
			SystemUserVo user = getCurrentSystemUser();
			if (user == null) {
				doResult(response, false, StatusCode.LOGINOUT, null, ResultType.LOGINOUT);
				return null;
			}
			List<Tree> temp = menuService.findList(classify);
			for (Tree tree : temp) {
				if (tree.getChildren().size() == 0) {
					tree.setChildren(null);
				} else {
					List<Tree> temp2 = tree.getChildren();
					for (Tree tree2 : temp2) {
						if (tree2.getChildren().size() == 0) {
							tree2.setChildren(null);
						} else {
							List<Tree> temp3 = tree2.getChildren();
							for (Tree tree3 : temp3) {
								if (tree3.getChildren().size() == 0) {
									tree3.setChildren(null);
								}
							}
						}
					}
				}
			}
			return temp;
		} catch (Exception e) {
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Description:新增菜单的信息
	 * 
	 * @param request
	 * @param response
	 * @param menuEntity
	 * @author shaoqiping
	 * @date 2017年8月16日下午2:48:27
	 */
	@RequiresRoles(UserType.SYSTEM)
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public void insert(HttpServletRequest request, HttpServletResponse response, MenuEntity menuEntity,
			Integer classify) {
		try {
			// 条件判断
			if (menuEntity == null || StringUtils.isEmpty(menuEntity.getName())) {
				doResult(response, false, StatusCode.LACKVALUE, null, "菜单名称不能为空！");
				return;
			}
			menuEntity.setId(null);
			if (menuEntity.getParentId() == null) {
				menuEntity.setType(0);
				menuEntity.setNumber("0");
			} else {
				MenuEntity parent = menuService.find(menuEntity.getParentId());
				if (parent.getType() == 0) {
					menuEntity.setType(1);
				} else if (parent.getType() == 1) {
					menuEntity.setType(2);
				} else {
					doResult(response, false, StatusCode.SAVEERROR, null, "所选菜单为按钮，无法添加下级");
					return;
				}
				menuEntity.setNumber(parent.getNumber() + "_" + parent.getId());
			}
			menuEntity.setClassify(classify);
			boolean flag = menuService.insertSelective(menuEntity);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.ADD_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.ADD_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:更改某个菜单的信息
	 * 
	 * @param request
	 * @param response
	 * @param menuEntity
	 * @author shaoqiping
	 * @date 2017年8月16日下午3:38:29
	 */
	@RequiresRoles(UserType.SYSTEM)
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public void update(HttpServletRequest request, HttpServletResponse response, MenuEntity menuEntity) {
		try {
			// 条件判断
			if (menuEntity == null || menuEntity.getId() == null || menuEntity.getId() <= 0) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择要修改的菜单！");
				return;
			}
			boolean flag = menuService.updateByPrimaryKeySelective(menuEntity);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:上下移动控件
	 * 
	 * @param request
	 * @param response
	 * @author shaoqiping
	 * @date 2017年8月17日上午10:05:06
	 */
	@RequiresRoles(UserType.SYSTEM)
	@RequestMapping(value = "/move", method = RequestMethod.POST)
	@ResponseBody
	public void move(HttpServletRequest request, HttpServletResponse response, Long id, Integer seq, Long changeId,
			Integer changeSeq) {
		try {
			if (id == null || LongUtils.isEmpty(id) || changeId == null || LongUtils.isEmpty(changeId) || seq == null
					|| changeSeq == null) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择要移动的菜单！");
				return;
			}
			boolean flag = menuService.move(id, seq, changeId,changeSeq);
			doResult(response, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, null,
					flag ? ResultType.SAVE_SUCCESS : "已到达顶部或底部");
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:删除某个菜单的信息
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @author shaoqiping
	 * @date 2017年8月16日下午4:23:56
	 */
	@RequiresRoles(UserType.SYSTEM)
	@RequestMapping(value = "/deletes")
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) Long id,
			@RequestParam(value = "number", required = true) String number) {
		try {
			if (id == null || LongUtils.isEmpty(id) || number == null || StringUtils.isEmpty(number)) {
				doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.CHOOSE_INFO);
				return;
			}
			boolean flag = menuService.sysDeletes(id, number);
			if (flag) {
				doResult(response, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

}
